home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14539 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  108 lines

  1. Path: newsfeed.internetmci.com!panix!not-for-mail
  2. From: acinader@panix.com (Arthur Cinader Jr)
  3. Newsgroups: panix.questions,gnu.g++.help,comp.lang.c++,alt.uu.math.misc
  4. Subject: All I want is a Triangle!?! (C/C++)
  5. Followup-To: gnu.g++.help
  6. Date: 30 Mar 1996 19:02:04 -0500
  7. Organization: Panix
  8. Message-ID: <4jki1s$snd@panix.com>
  9. NNTP-Posting-Host: panix.com
  10.  
  11.  
  12. Note followups...may not be appropriate, but at least it has
  13. help in the name :}
  14.  
  15. I am trying to develop an algorithm to take the following
  16. arguments to describe an equalateral triangle:
  17.  
  18. Coordinates of tip - (X,Y)
  19. Length of side - Side
  20.  
  21. My algorithm produces a very ugly and extremely inaccurate
  22. trinagle!  I have put the code next.  It uses curses.  An
  23. explanation of my algorithm is after the code...
  24.  
  25.  
  26. *******
  27. Start code
  28. ******
  29. #include <curses.h>
  30. #include <math.h>
  31.  
  32. int main()
  33. {
  34.     int x1, y1;
  35.     float x, y, side, m, b;
  36.     double A, B, C; /* Sides of right triangle ABC */
  37.  
  38.     initscr();
  39.  
  40.     x = 40;         /* x value of the top */
  41.     y = 1;          /* y value of the top */
  42.     side = 15;      /* length of side */
  43.  
  44.     C = side;       /* The hypotenues of ABC */
  45.     B = side / 2;
  46.     A = sqrt(pow(C,2) - pow(B,2));  /* The height of 
  47.                                                 the equalateral triangle */
  48.     m = - A / B;    /* slope of line */
  49.     b = y - m * x;  /* y intercept */
  50.         
  51.      /* loop the y values */
  52.     for (y1 = (int) y; y1 < A + y; y1++)  
  53.           /* loop the x values */
  54.         for (x1 = (int) ((y1 - b) / m); 
  55.                     x1 < x + 2 * (x - (y1 - b) / m); x1++)  /* loop the x values */
  56.            mvaddch(y1, x1, '*');
  57.  
  58.         addch('\n');
  59.         printw("X = %.1f  Y = %.1f Side = %.1f\n", x, y, side);
  60.         printw("C = %f\n", C);
  61.         printw("B = %f\n", B);
  62.         printw("A = %f\n", A);
  63.         printw("m = %f\n", m);
  64.         printw("b = %f\n", b);
  65.         printw("Thanks!");
  66.  
  67.         refresh();
  68.         endwin();
  69.  
  70.         return 0;
  71. }
  72.  
  73. ********
  74. end code
  75. ********
  76. here's the explanation -- paper and pencil will help!
  77.  
  78. My algorithm goes something like this:
  79.  
  80. An equalateral trinagle is two right triangles 
  81. The right triangles can be described as ABC
  82. Tri ABC has hypoteneus C of length Side
  83. Tri ABC has leg B of 1/2 Side
  84. Tri ABC has leg A of sqrt(C^2 - b^2) 
  85. Leg A is the height of the equalateral triangle I am trying to
  86. solve for.
  87.  
  88. If we now call the equalateral triangle DEF, and side F is
  89. opposite the tip, then the lines D and E can be described with
  90. the formula y = mx + b.
  91.  
  92. Since I now have all the info I need to find any point x along
  93. the sides of the equalateral triangle, and I have the height
  94. of the equalateral triangle, if I loop through each y and
  95. calculate the value of x, I should be able to describe the
  96. trinagle...
  97.  
  98. The code should do this...I have spent an entire day
  99. pencil checking this, and it should work, but it doesn't.  
  100.  
  101. Is there something about integers, floats, and computers that
  102. I am missing, or is my algorithm flawed and I'm too dim to see
  103. it?
  104.  
  105. Arthur
  106.  
  107. P.S. The circle is next!
  108.